package code.main.view.table
{
	import code.common.assets.AssetsClassName;
	
	import com.yy.manager.ResourceManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Video;
	import flash.utils.Timer;

	public class Clock extends Sprite
	{
		private var _clockUi:MovieClip;
		private var _timer:Timer;
		private var _t:Number = 0;
		private var _viseble:Boolean=false;
		public function Clock()
		{
			_clockUi = ResourceManager.getMovieClipByName(AssetsClassName.ClockUi);
			_clockUi.mcClockSecondTen.gotoAndStop(1);
			_clockUi.mcClockSecondOne.gotoAndStop(1);
		}
		
		/**
		 *设置显示时间 
		 * @param t
		 * 
		 */
		public function setTime(t:int):void {
			dispose();
			_t = t;
			_timer = new Timer(1000, t);
			_timer.addEventListener(TimerEvent.TIMER, timerFunc);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteFunc);
			_timer.start();
			clockVisible = true;
			showUi(t);
		}
		
		/**
		 *倒计时完成 
		 * @param e
		 * 
		 */
		private function timerCompleteFunc(e:Event=null):void {
			dispose();
			clockVisible = false;
		}
		
		/**
		 * 
		 * @param e
		 * 
		 */
		private function timerFunc(e:Event):void {
			_t--; 
			showUi(_t);
		}
		/**
		 *设置闹钟剩余时间显示 
		 * @param t
		 * 
		 */
		private function showUi(t:int):void {
			_clockUi.mcClockSecondTen.gotoAndStop(int(t/10)+1);
			_clockUi.mcClockSecondOne.gotoAndStop(t%10+1);
		}
		
		/**
		 * 设置闹钟显示
		 * @param boo
		 * 
		 */
		public function set clockVisible(boo:Boolean):void {
			if(boo) {
				addChild(_clockUi);
				this._viseble=true;
			}else {				
				if(this._viseble)
				removeChild(_clockUi);
				this._viseble=false;
				dispose();
			}
		}
		
		/**
		 *释放资源 
		 * 
		 */
		private function dispose():void {
			if(_timer) {
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, timerFunc);
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteFunc);
				_timer = null;
			}
		}
	}
}